package com.example.controller;
import com.example.common.Result;
import com.example.exception.CustomException;
import com.example.pojo.Room;
import com.example.pojo.RoomImage;
import com.example.service.RoomImageService;
import com.example.service.RoomService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/room")
public class RoomController {
    @Resource
    private RoomService roomService;

    @Resource
    private RoomImageService roomImageService;

    /**
     * 添加房间
     * @param room
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Room room) {
        roomService.add(room);

        return Result.success(room);
    }

    /**
     * 修改房间
     * @param room
     * @return
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody Room room) {
        roomService.updateById(room);
        return Result.success();
    }

    /**
     * 删除房间
     * @return
     */
    @DeleteMapping("/delete")
    public Result deleteById(@RequestParam Integer id) {
        roomService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除房间
     * @return
     */
    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        roomService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 查询所有房间
     *
     * @return
     */
    @GetMapping("/selectAll")
    public Result selectAll(@RequestHeader("Authorization") String token,@RequestParam(required = false) String keyword,
                            @RequestParam(required = false)String status, @RequestParam(required = false)Integer typeId,
                            @RequestParam(required = false) Integer roomId){
        List<Room> list = roomService.selectAll(token,keyword,status,typeId,roomId);
        return Result.success(list);
    }

    /**
     * 查询所有房间图片
     *
     * @return
     */
    @GetMapping("/selectAllImagesById")
    public Result selectAllImagesById(Room room) {
        List<RoomImage> list = roomImageService.selectById(room.getId());
        if (list.size()>0)
        return Result.success(list);
        else return Result.error("401","未找到该图片");
    }

//    @GetMapping("/selectPhoneById")
//    public Result selectPhoneById(Integer id) {
//        Room Room = roomService.selectById(id);
//        return Result.success(Room);
//    }

    /**@PathVariable的查询方式/selectById/1(/..)
     * 根据id查询用户
     * @return
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Room Room = roomService.selectById(id);
        return Result.success(Room);
    }

    /**@RequestParam的查询方式/selectPage?pageNum=1(&...=...)
     * @RequestParam(defaultValue = "1")默认值为1
     *@RequestParam(required = false)不是必须要传参
     * 分页查询
     */
    @GetMapping("/SelectPage")
    public Result selectPage(Room room,@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "5") Integer pageSize,
                             @RequestHeader("Authorization") String token,
                             @RequestParam(required = false) String keyword,
                             @RequestParam(required = false)String status,
                             @RequestParam(required = false)Integer typeId) {
        PageInfo<Room> pageInfo= roomService.selectPage(room,pageNum,pageSize,token,keyword,status,typeId);
        return Result.success(pageInfo);
    }
}
